//
//  ViewController.m
//  Timer2
//
//  Created by LeeJay on 16/4/19.
//  Copyright © 2016年 LeeJay. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    [self timer];
    
}

- (void)timer {
    
    // timerWithTimeInterval创建的定时器需要手动加入CurrentRunLoop，运行在指定Modes下，否则不会启动
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
                                             target:self
                                           selector:@selector(run)
                                           userInfo:nil
                                            repeats:YES];
    // 定时器只运行在NSDefaultRunLoopMode下，一旦RunLoop进入其他模式，这个定时器就不会工作
//     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    // 定时器只运行在UITrackingRunLoopMode下，一旦RunLoop进入其他模式，这个定时器就不会工作
//     [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
    
    
    // 定时器会运行在CommonModes下，CommonModes只是一种占位用的Mode，不是一种真正的Mode
    // 标记为CommonModes的模式有NSDefaultRunLoopMode和UITrackingRunLoopMode
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    NSLog(@"%@",[NSRunLoop currentRunLoop]);
}

- (void)run {
    
    NSLog(@"----run----");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
